from selenium.webdriver import Chrome
import time

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.select import Select

webdriver = Chrome(executable_path='/Users/sonto/Downloads/webdriver/79/chromedriver')
# tutorial
# webdriver.get("https://movie.douban.com/")
# time.sleep(10)
#
# items = webdriver.find_elements_by_xpath("//*[@id='content']/div/div[2]/div[3]/div[3]/div/div[1]/div/div[2]/a[@class='item']")
# for i in items:
#     assert isinstance(i, WebElement)
#     # print(i.tag_name, i.get_attribute("class"))
#     film_name = i.find_element_by_tag_name("p")
#     rate = i.find_element_by_xpath("p/strong")
#     print(film_name.text, rate)

# input
# webdriver.get("https://www.douban.com")
# search_input = webdriver.find_element_by_xpath("//*[@id='anony-nav']/div[2]/form/span[1]/input")
# search_input.send_keys("电影xxxxx")
# time.sleep(1)
# search_input.send_keys(Keys.BACKSPACE)
# search_input.send_keys(Keys.BACKSPACE)
# search_input.send_keys(Keys.BACKSPACE)
# search_input.send_keys(Keys.BACKSPACE)
# search_input.send_keys(Keys.BACKSPACE)

# search_input.submit()
# time.sleep(5)

# input + button
# webdriver.get("https://www.baidu.com")
# search = webdriver.find_element_by_id("kw")
# search.send_keys("疫情")
# submit = webdriver.find_element_by_id("su")
# # submit.submit()
# submit.click()
#
# webdriver.get("http://127.0.0.1:8000/login/")
# # 方法一
# # select_element = webdriver.find_element_by_tag_name("select")
# # options = select_element.find_elements_by_tag_name("option")
# # options[2].click()
# # time.sleep(10)
#
# # 方法二
# select_obj = Select(webdriver.find_element_by_tag_name("select"))
# # select_obj.select_by_visible_text("Audi")
# # time.sleep(5)
#
# # 方法三
# # select_obj.select_by_index(2)
# select_obj.select_by_value("saab")
#
#
#
# time.sleep(5)

# webdriver.get("https://www.baidu.com")
webdriver.get("https://www.douban.com")
print("Windows: {}".format(len(webdriver.window_handles)))
m = webdriver.find_element_by_class_name('lnk-movie')
m.click()
print("Windows: {}".format(len(webdriver.window_handles)))

time.sleep(2)

webdriver.switch_to.window(webdriver.window_handles[0])

# webdriver.switch_to_window()
# webdriver.back() # back to baidu
# time.sleep(2)
# webdriver.forward() # forward to douban
# time.sleep(5)
# for win in webdriver.window_handles:
#     print(type(win))


time.sleep(5)
webdriver.close()